<template>
  <div class="app-container">


    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          size="mini"
          type="text"
          icon="el-icon-edit"
          @click="saveSet()"
        >保存设置</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="columnList" @selection-change="handleSelectionChange">
      <el-table-column label="列表展示" align="center" prop="showInList">
        <template slot-scope="scope" >
          <el-switch v-model="scope.row.showInList"></el-switch>
        </template>
      </el-table-column>
     <!-- <el-table-column label="检索项" align="center" prop="showInList">
        <template slot-scope="scope" >
          <el-switch v-model="scope.row.showInSearch"></el-switch>
        </template>
      </el-table-column>-->
      <el-table-column label="列描述" align="center" prop="columnComment" />
      <el-table-column label="列名称" align="center" prop="columnName" />
      <el-table-column label="类型" align="center" prop="java_type" />
      <el-table-column label="移动" align="center">
        <template slot-scope="scope" >
          <el-button  type="primary" :disabled="scope.$index === 0" @click="moveUp(scope.$index)">上移</el-button>
          <el-button  type="success" :disabled="scope.$index === columnList.length - 1" @click="moveDown(scope.$index)">下移</el-button>
        </template>
      </el-table-column>


    </el-table>


  </div>
</template>

<script>
import { getColumnListAll,saveListSet } from "@/api/tool/gen";

export default {
  name: "Column",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 单字段表格数据
      columnList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        componentId: null,
        componentType: null,
        label: null,
        defineId: null,
        createUserId: null,
        createUserName: null,
        updateUserId: null,
        updateUserName: null,
        isDelete: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      formDefineId:null
    };
  },
  created() {
    this.formDefineId = this.$route.query.defineId
    this.getList();
  },
  mounted() {
    this.formDefineId = this.$route.query.defineId
    this.getList();
  },
  methods: {
    /** 查询单字段列表 */
    getList() {
      this.loading = true;
      this.queryParams.defineId=this.formDefineId
      console.log(this.formDefineId)
      getColumnListAll(this.formDefineId).then(response => {
        console.log(response)
        this.columnList = response.data;

        this.total = response.data.length;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        componentId: null,
        componentType: null,
        label: null,
        defineId: null,
        createUserId: null,
        createTime: null,
        createUserName: null,
        updateUserId: null,
        updateTime: null,
        updateUserName: null,
        isDelete: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 上移
    moveUp (index) {
      const arr = this.columnList
      arr.splice(index - 1, 1, ...arr.splice(index, 1, arr[index - 1]))
    },
// 下移
    moveDown (index) {
      const arr = this.columnList
      arr.splice(index, 1, ...arr.splice(index + 1, 1, arr[index]))
    },
    saveSet(){
      saveListSet(this.columnList).then(res=>{
           this.$message.success("已保存")
         })
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加单字段";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getColumn(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改单字段";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateColumn(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addColumn(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

  }
};
</script>
